/*
 * @(#)AssMouseEvent.java   09/12/30
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package fr.ema.arch3d.editor.ass;

import fr.ema.arch3d.editor.Evolution;

import org.jgraph.JGraph;
import org.jgraph.graph.BasicMarqueeHandler;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class AssMouseEvent extends BasicMarqueeHandler implements MouseListener {

    /** Field description */
    public List cmpIns_list;

    /** Field description */
    public Component container;

    /** Field description */
    public List ctrIns_list;

    /** Field description */
    public DrawAssImpl da;

    /** Field description */
    String oldCmpName;

    /**
     * Constructs ...
     *
     *
     * @param da
     */
    public AssMouseEvent(DrawAssImpl da) {
        this.da   = da;
        container = da.container;
    }

    /**
     * Method description
     *
     * @param e  
     */
    public void mouseClicked(MouseEvent e) {
        if ((e.getClickCount() == 1) && (e.getButton() == MouseEvent.BUTTON1)) {
            JGraph    graph     = getGraphForEvent(e);
            int       x         = e.getX(),
                      y         = e.getY();
            Object    cell      = graph.getFirstCellForLocation(x, y);
            Component container = da.container;

            try {
                this.cmpIns_list = Fractal.getAssemblyDescriptorController(container).getCmpInsList();
                this.ctrIns_list = Fractal.getAssemblyDescriptorController(container).getCtrInsList();
            } catch (NoSuchInterfaceException e1) {

                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            if ((cell != null) && cell.getClass().getSimpleName().equals("SpecVertex")) {
                for (int i = 0; i < this.cmpIns_list.size(); i++) {
                    if (((String[]) this.cmpIns_list.get(i))[0].equals(cell.toString())) {
                        AssADLText t = new AssADLText();

                        // da.aText.setDocument(t.createTextCmp((Component)config.component_list.get(i)));
                        da.aText.updateUI();
                    }
                }
            }

            if ((cell != null) && cell.getClass().getSimpleName().equals("SpecEdge")) {
                for (int i = 0; i < this.ctrIns_list.size(); i++) {
                    if (((String[]) this.ctrIns_list.get(i))[0].equals(cell.toString())) {
                        AssADLText t = new AssADLText();

                        // da.aText.setDocument(t.createTextCnt((Connector)config.connector_list.get(i)));
                        da.aText.updateUI();
                    }
                }
            } else {

                // System.out.println("here");
                AssADLText t = new AssADLText();

                da.aText.setDocument(t.createTextAss(container));
                da.aText.updateUI();
            }
        }
    }

    /**
     * Method description
     *
     * @param e  
     */
    public void mouseEntered(MouseEvent e) {}

    /**
     * Method description
     *
     * @param e  
     */
    public void mouseExited(MouseEvent e) {

        // TODO Auto-generated method stub
    }

    /**
     * Method description
     *
     * @param e  
     */
    public void mousePressed(MouseEvent e) {
        if ((e.getClickCount() == 1) && (e.getButton() == MouseEvent.BUTTON3)) {

            // startPoint = e.getPoint();
            JGraph graph = getGraphForEvent(e);

            // CellView[] views = graph.getGraphLayoutCache().getCellViews();
            // Get Cell under Mousepointer
            int               x                 = e.getX(),
                              y                 = e.getY();
            Object            cell              = graph.getFirstCellForLocation(x, y);
            ActionListener    actionListener    = new PopupActionListener();
            PopupMenuListener popupMenuListener = new MyPopupMenuListener();
            JPopupMenu        menu              = new JPopupMenu();

            // Print Cell Label
            if ((cell != null) && cell.getClass().getSimpleName().equals("AssVertexAss")) {
                if (((AssVertexAss) cell).vertexType.equals("component")) {
                    oldCmpName = ((AssVertexAss) cell).vertexName;

                    JMenuItem replace   = null;
                    JMenuItem deleteCmp = null;

                    menu.add(replace = new JMenuItem("Replace component instance"));
                    replace.addActionListener(actionListener);
                    menu.add(deleteCmp = new JMenuItem("Delete component instance"));
                } else if (((AssVertexAss) cell).vertexType.equals("connector")) {
                    JMenuItem deleteCtr = null;

                    menu.add(deleteCtr = new JMenuItem("Delete connector instance"));
                }

                // Display PopupMenu
                // item1.setHorizontalTextPosition(JMenuItem.RIGHT);
                // item.addActionListener(menuListener);
                menu.show(graph, e.getX(), e.getY());
            } else {

                // Display PopupMenu
                JMenuItem item;

                menu.add(item = new JMenuItem("Insert component instance"));
                menu.add(item = new JMenuItem("Insert connector instance"));
                item.setHorizontalTextPosition(JMenuItem.RIGHT);

                // item.addActionListener(menuListener);
                menu.show(graph, e.getX(), e.getY());
            }

            menu.addPopupMenuListener(popupMenuListener);
        }
    }

    /**
     * Method description
     *
     * @param e  
     */
    public void mouseReleased(MouseEvent e) {

        // TODO Auto-generated method stub
    }

    /**
     * Class description
     *
     *
     * @version        1.0, 09/09/13
     * @author         Huaxi (Yulin) ZHANG
     */
    class MyPopupMenuListener implements PopupMenuListener {

        /**
         * Method description
         *
         * @param popupMenuEvent  
         */
        public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) {
            System.out.println("Canceled");
        }

        /**
         * Method description
         *
         * @param popupMenuEvent  
         */
        public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) {

            // System.out.println("Becoming Invisible"+popupMenuEvent);
        }

        /**
         * Method description
         *
         * @param popupMenuEvent  
         */
        public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {

            // System.out.println("Becoming Visible");
        }
    }


    /**
     * Class description
     *
     *
     * @version        1.0, 09/09/13
     * @author         Huaxi (Yulin) ZHANG
     */
    class PopupActionListener implements ActionListener {

        /**
         * Method description
         *
         * @param e  
         */
        public void actionPerformed(ActionEvent e) {

            // System.out.println("Selected: " + e.getActionCommand());
            if (e.getActionCommand().equals("Replace component instance")) {
                Evolution evolution = new Evolution("ass", "replace", oldCmpName);

                // AssEvolve.cmpReplace(oldCmpName);
            }
        }
    }
}
